###### Title  ##########################################################
## Candidate Qualifications and Out-Group Support:
## Evidence from Afghanistan by Jasmine Bhatia and
## Steve L. Monroe.
## Replication Command File for Conjoint Analysis: SI Material 
## October 2024

# This file contains analysis and tables of the conjoint survey experiment
# presented in the Supplementary Information (SI)


## Table of Contents ########
## 1. Upload Data
## 2. Summary Statistics on Respondents Preferences 
## 4. Qualifications and Support for Female Candidates 
## 5. Qualifications and Support for Hazara Candidates 
## 6. Qualifications and Support for Female Hazara Candidates
## 7. Discussion Analysis


## 1. Upload Data, Install Packages #####

# load necessary packages

library(cjoint)
library(dplyr)
library(tidyr)
library(tidyverse)
library(survey)
library(Rcpp)
library(estimatr)
library(knitr)
library(here)
library(cli)
library(devtools)
devtools::install_github("m-freitag/cjpowR")
library(cjpowR)
library("xtable")
library("base")
library("cregg")


# set working directory and load dataset

here()

data <- read.csv("Afghan_Data_All.csv")


## 2. SI Section 3, Table 1: Respondents' Preferred Leadership Characteristics ####


balance <- data[, c("ID","Res_Gender", "Literacy", "Employ_Unem", "Res_Ethnicity",
                    "TravelEase", "HH_Head_Edu", "Res_Education",
                    "Province", "Att_Afg_Mil", "Behav_Peace",
                    "Att_Gov_Exp", "Att_Muj", "Behav_law_punish", "Behav_ethnic",
                    "Att_Strong_Rel", "Att_Well_Ed", "Res_Age")]



balance <- unique(balance)


balance$Behav_Peace_new <- case_when(
  balance$Behav_Peace == 1 ~ 6,
  balance$Behav_Peace  == 2 ~ 5, 
  balance$Behav_Peace == 3 ~ 4, 
  balance$Behav_Peace == 4 ~ 3,
  balance$Behav_Peace == 5 ~ 2,
  balance$Behav_Peace== 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Behav_law_punish_new <- case_when(
  balance$Behav_law_punish == 1 ~ 6,
  balance$Behav_law_punish  == 2 ~ 5, 
  balance$Behav_law_punish == 3 ~ 4, 
  balance$Behav_law_punish == 4 ~ 3,
  balance$Behav_law_punish== 5 ~ 2,
  balance$Behav_law_punish == 6 ~ 1,
  TRUE ~ NA_real_
)



balance$Behav_ethnic_new <- case_when(
  balance$Behav_ethnic == 1 ~ 6,
  balance$Behav_ethnic  == 2 ~ 5, 
  balance$Behav_ethnic == 3 ~ 4, 
  balance$Behav_ethnic == 4 ~ 3,
  balance$Behav_ethnic == 5 ~ 2,
  balance$Behav_ethnic == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Religious_new <- case_when(
  balance$Att_Strong_Rel == 1 ~ 6,
  balance$Att_Strong_Rel  == 2 ~ 5, 
  balance$Att_Strong_Rel== 3 ~ 4, 
  balance$Att_Strong_Rel == 4 ~ 3,
  balance$Att_Strong_Rel == 5 ~ 2,
  balance$Att_Strong_Rel== 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Gov_Exp_new <- case_when(
  balance$Att_Gov_Exp == 1 ~ 6,
  balance$Att_Gov_Exp  == 2 ~ 5, 
  balance$Att_Gov_Exp == 3 ~ 4, 
  balance$Att_Gov_Exp == 4 ~ 3,
  balance$Att_Gov_Exp == 5 ~ 2,
  balance$Att_Gov_Exp == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Afg_Mil_new <- case_when(
  balance$Att_Afg_Mil == 1 ~ 6,
  balance$Att_Afg_Mil  == 2 ~ 5, 
  balance$Att_Afg_Mil == 3 ~ 4, 
  balance$Att_Afg_Mil == 4 ~ 3,
  balance$Att_Afg_Mil == 5 ~ 2,
  balance$Att_Afg_Mil == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Afg_Muj_new <- case_when(
  balance$Att_Muj == 1 ~ 6,
  balance$Att_Muj  == 2 ~ 5, 
  balance$Att_Muj == 3 ~ 4, 
  balance$Att_Muj == 4 ~ 3,
  balance$Att_Muj == 5 ~ 2,
  balance$Att_Muj == 6 ~ 1,
  TRUE ~ NA_real_
)


balance$Military_exp <- (balance$Afg_Muj_new + balance$Afg_Mil_new) / 2


balance$Att_Well_Ed_new <- case_when(
  balance$Att_Well_Ed == 1 ~ 6,
  balance$Att_Well_Ed  == 2 ~ 5, 
  balance$Att_Well_Ed == 3 ~ 4, 
  balance$Att_Well_Ed == 4 ~ 3,
  balance$Att_Well_Ed == 5 ~ 2,
  balance$Att_Well_Ed == 6 ~ 1,
  TRUE ~ NA_real_
)



selected_vars <- c("Att_Well_Ed_new", "Military_exp",
                   "Gov_Exp_new", "Religious_new", "Behav_Peace_new",
                   "Behav_law_punish_new") 
                   
                   
  

var_labels <-c("Well Educated", "Military Experience", "Government Experience",
               "Religious", "Provides Peace", "Punishes Criminals")
               


num_observations <- as.integer(colSums(!is.na(balance[selected_vars])))

# Create the summary table with variable names and labels
summary_table <- data.frame(
  Variable = var_labels,
  Obs = num_observations,
  Mean = round(apply(balance[selected_vars], 2, mean, na.rm = TRUE), 2),
  SD = round(apply(balance[selected_vars], 2, sd, na.rm = TRUE), 2),
  Min = round(apply(balance[selected_vars], 2, min, na.rm = TRUE), 2),
  Max = round(apply(balance[selected_vars], 2, max, na.rm = TRUE), 2)
)


table1 <- xtable(summary_table, caption = "Summary Statistics")
latex_table1 <- capture.output(print(table1, caption.placement = "top", 
                                   include.rownames = FALSE))

# Export to Overleaf

writeLines(latex_table1, 
           "/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/summ_stat.tex")


# T-test different meants between support for education for qualifications
# Referenced in manuscript section 4

t <- t.test(balance$Att_Well_Ed_new, balance$Military_exp,
       alternative = c("two.sided"),
       mu = 0, paired = FALSE, var.equal = FALSE,
       conf.level = 0.95)


# Mean support for education greater than military experience, p < 0.01



t.test(balance$Att_Well_Ed_new, balance$Gov_Exp_new,
       alternative = c("two.sided"),
       mu = 0, paired = FALSE, var.equal = FALSE,
       conf.level = 0.95)


# Mean support for education greater than gov experience, p < 0.01


## 3. SI Section 4. Candidate Qualifications and Men's Support for Female Leadership ####

data <- read.csv("conjoint_clean.csv")

# make every variable a factor for "cj" to work, 
# except for the choice and rating variable


data[, -which(names(data) %in% c("Choice", "Rating"))] <- 
  lapply(data[, -which(names(data) %in% c("Choice", "Rating"))], as.factor)   


# subset data 

male <- data[data$Res_Gender == "Male Respondent",]


# SI Table 2 (hand code)

gender <- cj(male, Choice ~ Gender,
             id = ~ID,
             estimate= "mm")

group <- c("Female Candidate", "Male Candidate")


table2 <- cbind(gender, group)


# SI Table 3 (hand code)

gender <- cj(male, Choice ~ Education_Gender_Leader,
             id = ~ID,
             estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

table3  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))


## SI Table 4 (hand code)

## Rating

table4 <- cj(male, Rating ~ Education_Gender_Leader,
                  id = ~ID,
                  estimate= "mm")



# F-Test results, report in text section 5  
f.test.table4 <- cj_anova(male, Rating  ~ Gender,
                        by = ~ high_educated
)



## SI Figure 1 (AMCE)

gender <- cj(male, Choice ~ Education_Gender_Leader,
           id = ~ID,
           estimate= "amce")


group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))

figure1 <- ggplot(data = as.data.frame(gender),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="AMCE", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a4_female_amce.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 2 (Foreign vs Afghan educated)


gender <- cj(male, Choice ~ Western_Education_Gender_Leader_2,
         id = ~ID,
         estimate= "mm")

group <- c("Female Candidate (Higher Educated, Afghanistan)", "Female Candidate (Higher Educated, Foreign)",
           "Male Candidate (Higher Educated, Afghanistan)", "Male Candidate (Higher Educated, Foreign)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")
           
           

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Female Candidate (Less Educated)",
                             "Male Candidate (Higher Educated, Afghanistan)",
                             "Male Candidate (Higher Educated, Foreign)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated, Afghanistan)",
                             "Female Candidate (Higher Educated, Foreign)"
                             ))



figure2 <- ggplot(data = as.data.frame(gender),
                 aes(x = estimate,
                     y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a6_female.pdf",
       width = 20, height = 13, units = "cm")



# F-Test results  with choice
f.test.figure2 <- cj_anova(male, Choice  ~ Gender,
                    by = ~ Western_Education_Gender_Leader_2
)


## SI Figure 3 Madrassa Gender Education 


gender <- cj(male, Choice ~ Madrassa_Gender_Leader,
              id = ~ID,
              estimate= "mm")

group <- c("Female Candidate (Madrassa Educated)", "Male Candidate (Madrassa Educated)",
           "Female Candidate (Non Madrassa Educated)", "Male Candidate (Non Madrassa Educated)")




gender  <- gender %>%
  mutate(group = fct_relevel(group, "Female Candidate (Madrassa Educated)",
                             "Male Candidate (Non Madrassa Educated)",
                             "Male Candidate (Madrassa Educated)",
                             "Female Candidate (Non Madrassa Educated)"
  ))



figure3 <- ggplot(data = as.data.frame(gender),
                      aes(x = estimate,
                          y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")


figure3

# export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a7_female.pdf",
       width = 20, height = 13, units = "cm")



# F-Test results with choice
f.test.figure3 <- cj_anova(male, Choice  ~ Gender,
                           by = ~  Madrassa_Gender_Leader
)


## SI Figure 4 (Corrected Standard Errors)


# install.packages("devtools")
devtools::install_github("yhoriuchi/projoint")

# this is from:
# https://github.com/yhoriuchi/projoint/blob/master/vignettes/02-wrangle.Rmd

library(projoint)


# Create ID2 variable for projoint analysis

data$ID2 <- as.integer(data$ID)


data$Age2 <- as.character(data$Age) #make all attributes characters (this works - tell Horiuchi)

attributes <- ("Education_Gender_Leader")

# task = pair
# profile is either one or two

data$profile_pair_number <- ifelse(data$profile == 1, 1,
                            ifelse(data$profile == 3, 1, 
                              ifelse(data$profile == 5, 1,
                                2)))

data$pair_number <- ifelse(data$Pair == "A", 1,
                        ifelse(data$Pair == "B", 2,
                       ifelse(data$Pair == "C", 3,
                        NA)))        

# make data only male respondents

male <- data %>% 
  mutate(Age = as.character(Age)) %>% 
  filter(Res_Gender == "Male Respondent")

# Check
male %>% count(pair_number) # tasks
male %>% count(profile_pair_number) # profiles
male %>% count(Choice) # outcome

# Reshape data for conjoint analysis 


cj_data <- make_projoint_data(.dataframe = male,
                              .attribute_vars = attributes, 
                              .id_var = "ID", # the default name 
                              .task_var = "pair_number", # the default name (profile number)
                              .profile_var = "profile_pair_number", # the default name (try: not work PairID, num;  before it profile_pair_number)
                              .selected_var = "Choice",
                              .selected_repeated_var = NULL,# the default name "selected_repeated"
                              .fill = TRUE)


# Estimate IRR without a repeated task 

predicted_irr <- predict_tau(cj_data)

# Receive warning sign when only one attribute, but can 
# continue to generate plot
print(predicted_irr)

# Estimate MMs 

out <- projoint(.data = cj_data,
                .irr = 0.702)

figure4 <- plot(out, .estimates = "both")

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/projoint_gender.pdf",
       width = 20, height = 13, units = "cm")


# SI Figure 5: High Educated Males Respondents


male_highedu <- male[male$Res_University == 1, ]

gender <- cj(male_highedu, Choice ~ Education_Gender_Leader,
         id = ~ID,
         estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))

figure5 <- ggplot(data = as.data.frame(gender),
                 aes(x = estimate,
                     y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")


figure5

# export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a8_female.pdf",
       width = 20, height = 13, units = "cm")


# SI Figure 6: Less educated male respondents


male_nohighedu <- male[male$Res_University == 0, ]

gender <- cj(male_nohighedu, Choice ~ Education_Gender_Leader,
             id = ~ID,
             estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))


figure6 <- ggplot(data = as.data.frame(gender),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
          labs(x="Marginal Means", y = "")+
          geom_vline(xintercept = 0.5, 
          linetype = "dashed")+
          ggtitle("")

figure6

## Export to overleaf
ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a9_female.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 7: Subset to high income males 


male_highincome <- male[male$Res_Highincome == 1, ]

gender <- cj(male_highincome, Choice ~ Education_Gender_Leader,
             id = ~ID,
             estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))


figure7 <- ggplot(data = as.data.frame(gender),
           aes(x = estimate,
            y = group)) + 
          geom_point(size = 3)+ 
          geom_pointrange(aes(xmin= estimate - 2*std.error,
           xmax=estimate + 2*std.error))+
          labs(x="Marginal Means", y = "")+
          geom_vline(xintercept = 0.5, 
          linetype = "dashed")+
          ggtitle("")


figure7

# Export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a10_female.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 8: Subset to low income males 

male_noincome <- male[male$Res_Highincome == 0, ]

gender <- cj(male_noincome, Choice ~ Education_Gender_Leader,
                id = ~ID,
                estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))


figure8 <- ggplot(data = as.data.frame(gender),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
            ggtitle("")


figure8


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a11_female.pdf",
       width = 20, height = 13, units = "cm")



### 4.  SI Section 5. Candidate Qualifications and Non-Hazara Support for Hazara Candidates ####

nonhazara <- data[data$Res_Hazara == 0, ]

# SI Table 5 (hand coded)
 
table5 <- cj(nonhazara, Choice ~ Hazara,
                id = ~ID,
                estimate= "mm")


# SI Table 6 (hand coded)


table6 <- cj(nonhazara, Choice ~ Education_Hazara_Leader,
             id = ~ID,
             estimate= "mm")


# F-Test results with choice reported in text Section 5.2
f.test.table6 <- cj_anova(nonhazara, Choice  ~ Hazara,
                          by = ~ Education_Hazara_Leader
)


# SI Table 7 (Rating, hand coded)


table7 <- cj(nonhazara, Rating ~ Education_Hazara_Leader,
             id = ~ID,
             estimate= "mm")


# put table on overleaf

# F-Test results with rating
f.test.table7 <- cj_anova(nonhazara, Rating  ~ Hazara,
                          by = ~ high_educated
)


## SI Figure 9 AMCE - estimations

ethnicity <- cj(nonhazara, Choice ~ Education_Hazara_Leader,
            id = ~ID,
            estimate= "amce")


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure9 <- ggplot(data = as.data.frame(ethnicity),
          aes(x = estimate,
           y = group)) + 
           geom_point(size = 3)+ 
           geom_pointrange(aes(xmin= estimate - 2*std.error,
          xmax=estimate + 2*std.error))+
          labs(x="Marginal Means", y = "")+
          geom_vline(xintercept = 0, 
          linetype = "dashed")+
          ggtitle("")


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b4_hazara_amces.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 10 Differences between Western vs. Non-Western educated


ethnicity <- cj(nonhazara, Choice ~ Western_Education_Hazara_Leader_2,
              id = ~ID,
              estimate= "mm")

group <- c("Hazara Candidate (Higher Educated, Afghanistan)", "Hazara Candidate (Higher Educated, Western)",
           "Non-Hazara Candidate (Higher Educated, Afghanistan)", "Non-Hazara Candidate (Higher Educated, Western)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")


ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Higher Educated, Afghanistan)",
                             "Non-Hazara Candidate (Higher Educated, Western)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated, Afghanistan)",
                             "Hazara Candidate (Higher Educated, Western)"
  ))



figure10 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
            ggtitle("")



figure10


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b5_hazara.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 11: Madrassa vs Non-Madrassa Educated 


ethnicity <- cj(nonhazara, Choice ~ Madrassa_Hazara_Leader,
                  id = ~ID,
                  estimate= "mm")

group <- c("Hazara Candidate (Madrassa Educated)", "Non-Hazara Candidate (Madrassa Educated)",
           "Hazara Candidate (Non Madrassa Educated)", 
           "Non-Hazara Candidate (Non Madrassa Educated)")


ethnicity <- ethnicity %>%
  mutate(group = fct_relevel(group, "Hazara Candidate (Madrassa Educated)",
                             "Non-Hazara Candidate (Non Madrassa Educated)",
                             "Non-Hazara Candidate (Madrassa Educated)",
                             "Hazara Candidate (Non Madrassa Educated)"
  ))



figure11 <- ggplot(data = as.data.frame(ethnicity),
                      aes(x = estimate,
                      y = group)) + 
                      geom_point(size = 3)+ 
                      geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
                      labs(x="Marginal Means", y = "")+
                      geom_vline(xintercept = 0.5, 
                      linetype = "dashed")+
                      ggtitle("")


figure11


## Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b6_hazara.pdf",
       width = 20, height = 13, units = "cm")



## Figure 12: Correct Standard Errors with Projoint

# make data only for non-hazaras


attributes <- c("Education_Hazara_Leader")

ethnicity <- data %>% 
  mutate(Age = as.character(Age)) %>% 
  filter(Res_Hazara == 0)

# Check
ethnicity %>% count(pair_number) # tasks
ethnicity %>% count(profile_pair_number) # profiles
ethnicity %>% count(Choice) # outcome


cj_data <- make_projoint_data(.dataframe = ethnicity,
                              .attribute_vars = attributes, 
                              .id_var = "ID", # the default name 
                              .task_var = "pair_number", # the default name (profile number)
                              .profile_var = "profile_pair_number", # the default name (try: not work PairID, num;  before it profile_pair_number)
                              .selected_var = "Choice",
                              .selected_repeated_var = NULL,# the default name "selected_repeated"
                              .fill = TRUE)


# Estimate IRR without a repeated task 
# 
predicted_irr <- predict_tau(cj_data)

# Keep going past the warning message

# Estimate MMs 

out <- projoint(.data = cj_data,
                .irr = 0.702)

figure12 <- plot(out, .estimates = "both")

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/projoint_ethnicity.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 13: High educated non hazara respondents Education 


nohaz_highedu <- nonhazara[nonhazara$Res_University == 1, ]


ethnicity <- cj(nohaz_highedu, Choice ~ Education_Hazara_Leader,
         id = ~ID,
         estimate= "mm")


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure13 <- ggplot(data = as.data.frame(ethnicity),
                 aes(x = estimate,
                y = group)) + 
                geom_point(size = 3)+ 
                geom_pointrange(aes(xmin= estimate - 2*std.error,
                xmax=estimate + 2*std.error))+
                labs(x="Marginal Means", y = "")+
                geom_vline(xintercept = 0.5, 
                linetype = "dashed")+
                ggtitle("")


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b14.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 14: Less educated non hazara respondents Education 


nohaz_noedu <- nonhazara[nonhazara$Res_University == 0, ]


ethnicity <- cj(nohaz_noedu, Choice ~ Education_Hazara_Leader,
                    id = ~ID,
                    estimate= "mm")


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure14 <- ggplot(data = as.data.frame(ethnicity),
                            aes(x = estimate,
                           y = group)) + 
                          geom_point(size = 3)+ 
                          geom_pointrange(aes(xmin= estimate - 2*std.error,
                          xmax=estimate + 2*std.error))+
                          labs(x="Marginal Means", y = "")+
                          geom_vline(xintercept = 0.5, 
                          linetype = "dashed")+
                          ggtitle("")


figure14


# Export to Overleaf
ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b15.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 15: High Income Non-Hazaras 


nohaz_highincome <- nonhazara[nonhazara$Res_Highincome == 1, ]



ethnicity <- cj(nohaz_highincome, Choice ~ Education_Hazara_Leader,
                  id = ~ID,
                  estimate= "mm")


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure15 <- ggplot(data = as.data.frame(ethnicity),
                          aes(x = estimate,
                          y = group)) + 
                          geom_point(size = 3)+ 
                          geom_pointrange(aes(xmin= estimate - 2*std.error,
                          xmax=estimate + 2*std.error))+
                          labs(x="Marginal Means", y = "")+
                          geom_vline(xintercept = 0.5, 
                          linetype = "dashed")+
                          ggtitle("")



figure15

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b16.pdf",
       width = 20, height = 13, units = "cm")


## Figure 16: Lower Income Non-Hazara respondents


nohaz_lowincome <- nonhazara[nonhazara$Res_Highincome == 0, ]


ethnicity <- cj(nohaz_lowincome, Choice ~ Education_Hazara_Leader,
                   id = ~ID,
                   estimate= "mm")


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure16 <- ggplot(data = as.data.frame(ethnicity),
                           aes(x = estimate,
                             y = group)) + 
                            geom_point(size = 3)+ 
                      geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
                      labs(x="Marginal Means", y = "")+
                      geom_vline(xintercept = 0.5, 
                      linetype = "dashed")+
                      ggtitle("")


figure16


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b17.pdf",
       width = 20, height = 13, units = "cm")



### 5. SI Section 6 Qualifications and Non-Hazara Men's Support for Out-Group Candidates ####


# Subset analysis to Male Non-Hazara Respondents

nhmale <- nonhazara[nonhazara$Res_Gender == "Male Respondent",]


# SI Table 8 (Hand Code)


table8 <- cj(nhmale, Choice ~ Hazara_Education_Gender_Leader,
            id = ~ID,
            estimate= "mm")


# SI Table 9 (Hand Code)

table9 <- cj(nhmale, Rating ~ Hazara_Education_Gender_Leader,
             id = ~ID,
             estimate= "mm")


# SI Figure 17: AMCE 


inter <- cj(nhmale, Choice ~ Hazara_Education_Gender_Leader,
                  id = ~ID,
                  estimate= "amce")


group <- c("Female Candidate (Hazara, Higher Educated)", "Male Candidate (Hazara, Higher Educated)",
           "Female Candidate (Non-Hazara, Higher Educated)", "Male Candidate (Non-Hazara, Higher Educated)",
           "Female Candidate (Hazara, Less Educated)", "Male Candidate (Hazara, Less Educated)",
           "Female Candidate (Non-Hazara, Less Educated)", "Male Candidate (Non-Hazara, Less Educated)")

inter <- inter %>%
  mutate(group = fct_relevel(group, "Male Candidate (Non-Hazara, Higher Educated)",
                             "Female Candidate (Hazara, Less Educated)",
                             "Male Candidate (Non-Hazara, Less Educated)",
                             "Male Candidate (Hazara, Higher Educated)",
                             "Female Candidate (Non-Hazara, Less Educated)",
                             "Male Candidate (Hazara, Less Educated)",
                             "Female Candidate (Non-Hazara, Higher Educated)",
                            "Female Candidate (Hazara, Higher Educated)"))


figure17 <- ggplot(data = as.data.frame(inter),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="AMCEs", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h3b_nhmale_amce.pdf",
       width = 20, height = 13, units = "cm")


# SI Figure 18: Corrected Standard Errors (projoint)


attributes <- c("Hazara_Education_Gender_Leader")

nhmale <- nhmale %>% 
  mutate(Age = as.character(Age)) 

# Check
nhmale  %>% count(pair_number) # tasks
nhmale  %>% count(profile_pair_number) # profiles
nhmale  %>% count(Choice) # outcome

# Reshape data for conjoint analysis 

cj_data <- make_projoint_data(.dataframe = nhmale,
                              .attribute_vars = attributes, 
                              .id_var = "ID", # the default name 
                              .task_var = "pair_number", # the default name (profile number)
                              .profile_var = "profile_pair_number", # the default name (try: not work PairID, num;  before it profile_pair_number)
                              .selected_var = "Choice",
                              .selected_repeated_var = NULL,# the default name "selected_repeated"
                              .fill = TRUE)


# Estimate IRR without a repeated task 
# 
predicted_irr <- predict_tau(cj_data)
# Keep going past the warning message
# Estimate MMs 

out <- projoint(.data = cj_data,
                .irr = 0.702)

figure18 <- plot(out, .estimates = "both")

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/projoint_inter.pdf",
       width = 20, height = 13, units = "cm")


### 6. SI Section Six Women, Hazaras and Pashtuns' Preferences #### 


# women's preferences

female <- data[data$Res_Gender == "Female Respondent",]

women <- cj(female, Choice ~ Gender,
                id = ~ID,
                estimate= "mm")

group <- c("Female Candidate", "Male Candidate")


women <- cbind(women, group)

figure19 <- ggplot(data = as.data.frame(women),
             aes(x = estimate,
               y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")

figure19

# export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_a.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 20: Gender and Qualifications

women <- cj(female, Choice ~ Education_Gender_Leader,
                id = ~ID,
                estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

women  <- women %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))



figure20 <- ggplot(data = as.data.frame(women),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")


figure20


# export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_b.pdf",
       width = 20, height = 13, units = "cm")



## Do F-Test

# F-Test results  with choice
f.test.women <- cj_anova(female, Choice  ~ Gender,
                             by = ~ high_educated
)


## Rating

women <- cj(female, Rating ~ Education_Gender_Leader,
                       id = ~ID,
                       estimate= "mm")



# F-Test results with choice
f.test.women.rating <- cj_anova(female, Rating  ~ Gender,
                             by = ~ high_educated
)



## SI Figure 21: Differences in qualifications among male and female candidates

female$high_educated_reorder <- relevel(female$high_educated, ref = "Low")


women <- cj(female, Choice ~ Gender,
                  id = ~ ID, estimate = "mm_differences",
                  by = ~ high_educated_reorder,
                  level_order = "ascending") # high educated female > low educaated female


group <- c("Female Candidate", "Male Candidate")



women  <- women %>%
  mutate(group = fct_relevel(group, "Female Candidate",
                             "Male Candidate"))


figure21 <- ggplot(data = as.data.frame(women),
                           aes(x = estimate,
                               y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (High - Low Qualifications)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



figure21


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_c.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 22


women <- cj(female, Choice ~ high_educated_reorder,
             id = ~ ID, estimate = "mm_differences",
             by = ~ Gender,
             level_order = "ascending") 


group <- c("Less Qualified Candidate", "High Qualified Candidate")

women  <- women  %>%
  mutate(group = fct_relevel(group, 
                             "Less Qualified Candidate",
                             "High Qualified Candidate"))


figure22 <- ggplot(data = as.data.frame(women),
                   aes(x = estimate,
                       y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (Male - Female)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



figure22


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h3a_5.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 23: AMCEs 


women <- cj(female, Choice ~ Education_Gender_Leader,
            id = ~ID,
            estimate= "amce")


group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")
           
           

women  <- women %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"
                             ))


figure23 <- ggplot(data = as.data.frame(women),
                   aes(x = estimate,
                   y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="AMCEs", y = "")+
                  geom_vline(xintercept = 0, 
                  linetype = "dashed")+
                  ggtitle("")


## Export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_female_amce.pdf",
       width = 20, height = 13, units = "cm")


## Hazara Respondents


hazara <- data[data$Res_Hazara == 1, ]


ethnicity <- cj(hazara, Choice ~ Education_Hazara_Leader,
                id = ~ID,
                estimate= "mm")


# F-Test 
f.test.ethnicity <- cj_anova(hazara, Choice  ~ Hazara,
                       by = ~ Education_Hazara_Leader
)

# stat significant


# F-Test rating
f.test.ethnicity.rating <- cj_anova(hazara, Rating  ~ Hazara,
                             by = ~ high_educated
)

# stat significant


## SI Figure 24

group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure24 <- ggplot(data = as.data.frame(ethnicity),
                        aes(x = estimate,
                         y = group)) + 
                        geom_point(size = 3)+ 
                        geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
                    labs(x="Marginal Means", y = "")+
                    geom_vline(xintercept = 0.5, 
                    linetype = "dashed")+
                    ggtitle("")


figure24

# Export to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_d.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 25: Across Qualifications

hazara$high_educated_reorder <- relevel(hazara$high_educated, ref = "Low")


ethnicity <- cj(hazara, Choice ~ Hazara,
           id = ~ ID, estimate = "mm_differences",
           by = ~ high_educated_reorder,
           level_order = "ascending") # high educated hazara > low educaated hazara


group <- c("Hazara Candidate", "Non-Hazara Candidate")



ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Hazara Candidate",
                             "Non-Hazara Candidate"))


figure25 <- ggplot(data = as.data.frame(ethnicity),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (High - Low Qualifications)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



figure25


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_e.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 26: Across ethnicities


ethnicity <- cj(hazara, Choice ~ high_educated_reorder,
            id = ~ ID, estimate = "mm_differences",
            by = ~ Hazara,
            level_order = "ascending") 


group <- c("Less Qualified Candidate", "More Qualified Candidate")

ethnicity  <- ethnicity  %>%
  mutate(group = fct_relevel(group, 
                             "Less Qualified",
                             "High Qualified"))


figure26 <- ggplot(data = as.data.frame(ethnicity),
                   aes(x = estimate,
                       y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (Non-Hazara - Hazara)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



figure26


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h3b_3.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 27: AMCEs

ethnicity <- cj(hazara, Choice ~ Education_Hazara_Leader,
            id = ~ID,
            estimate= "amce")

group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")



ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"
  ))


figure27 <- ggplot(data = as.data.frame(ethnicity),
                   aes(x = estimate,
                  y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="AMCEs", y = "")+
                  geom_vline(xintercept = 0, 
                  linetype = "dashed")+
                   ggtitle("")


# Export on to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_hazara_amce.pdf",
       width = 20, height = 13, units = "cm")



## SI. Figure 28: Pashtuns' support for Pashtun and Non-Pashtun Respondents

# Subset to Pashtun respondents

pashtun <- data[data$Res_Pashtun == 1, ]


ethnicity <- cj(pashtun, Choice ~ Education_Pashtun_Leader,
           id = ~ID,
           estimate= "mm")


# F-Test results 
f.test.pashtun <- cj_anova(pashtun, Choice  ~ Pashtun,
                        by = ~ Education_Pashtun_Leader
)


f.test.rating.pashtun <- cj(pashtun, Rating ~ Education_Pashtun_Leader,
                  id = ~ID,
                  estimate= "mm")



group <- c("Non-Pashtun Candidate (Higher Educated)", "Pashtun Candidate (Higher Educated)",
           "Non-Pashtun Candidate (Less Educated)", "Pashtun Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Pashtun Candidate (Higher Educated)",
                             "Pashtun Candidate (Less Educated)",
                             "Non-Pashtun Candidate (Less Educated)",
                             "Pashtun Candidate (Higher Educated)"))

figure28 <- ggplot(data = as.data.frame(ethnicity),
              aes(x = estimate,
               y = group)) + 
              geom_point(size = 3)+ 
              geom_pointrange(aes(xmin= estimate - 2*std.error,
               xmax=estimate + 2*std.error))+
              labs(x="Marginal Means", y = "")+
              geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
              ggtitle("")


figure28

# export to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_f.pdf",
       width = 20, height = 13, units = "cm")


## 

## SI Figure 29: Pashtun Preferences marginal mean differences 

pashtun$high_educated_reorder <- relevel(pashtun$high_educated, ref = "Low")


ethnicity <- cj(pashtun, Choice ~ Pashtun,
           id = ~ ID, estimate = "mm_differences",
           by = ~ high_educated_reorder,
           level_order = "ascending") 


group <- c("Non-Pashtun Candidate", "Pashtun Candidate")



ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Pashtun Candidate",
                             "Non-Pashtun Candidate"))


figure29 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Mean Differences (High - Low Qualifications)", y = "")+
            geom_vline(xintercept = 0, 
            linetype = "dashed")+
            ggtitle("")



figure29

# Export to Overelaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_g.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 30: Pasthun Preferences with AMCEs

ethnicity <- cj(pashtun, Choice ~ Education_Pashtun_Leader,
            id = ~ID,
            estimate= "amce")


group <- c("Non-Pashtun Candidate (Higher Educated)", "Pashtun Candidate (Higher Educated)",
           "Non-Pashtun Candidate (Less Educated)", "Pashtun Candidate (Less Educated)")



ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, 
                             "Non-Pashtun Candidate (Higher Educated)",
                             "Pashtun Candidate (Higher Educated)",
                             "Pashtun Candidate (Less Educated)",
                             "Non-Pashtun Candidate (Less Educated)",
                             
  ))


figure30 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
             geom_pointrange(aes(xmin= estimate - 2*std.error,
              xmax=estimate + 2*std.error))+
            labs(x="AMCEs", y = "")+
            geom_vline(xintercept = 0, 
             linetype = "dashed")+
              ggtitle("")

figure30


# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_pashtun_amce.pdf",
       width = 20, height = 13, units = "cm")



## 7. SI Section 7: Other Ethnic Groups ####


# SI Figure 31: Respondents' Preferences Across All Ethnic Groups

ethnicity <- cj(data, Choice ~ Ethnicity,
                id = ~ID,
                estimate= "mm")

group <- c("Hazara", "Pashtun", "Tajik", "Turkmen", "Uzbek")


ethnicity <- cbind(ethnicity, group)

figure31 <- ggplot(data = as.data.frame(ethnicity),
                   aes(x = estimate,
                       y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")


## Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_2.pdf",
       width = 20, height = 13, units = "cm")


## Non-Group Preferences

nonpashtun <- data[data$Res_Pashtun == 0, ]

## SI Fgure 32: Non-Pashtuns' Preferences 

ethnicity <- cj(nonpashtun, Choice ~ Pashtun,
         id = ~ID,
         estimate= "mm")

group <- c("Non-Pashtun Candidate", "Pashtun Candidate")


ethnicity <- cbind(ethnicity, group)

figure32 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
             linetype = "dashed")


figure32

# Export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_2_pashtun.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 33: Non-Pashtuns' Preferences Across Qualifications


ethnicity <- cj(nonpashtun, Choice ~ Education_Pashtun_Leader,
           id = ~ID,
           estimate= "mm")


f.test.nonpashtun <- cj_anova(nonpashtun, Choice  ~ Pashtun,
                        by = ~ Education_Pashtun_Leader
)



group <- c("Non-Pashtun Candidate (Higher Educated)", "Pashtun Candidate (Higher Educated)",
           "Non-Pashtun Candidate (Less Educated)", "Pashtun Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Pashtun Candidate (Higher Educated)",
                             "Pashtun Candidate (Less Educated)",
                             "Non-Pashtun Candidate (Less Educated)",
                             "Pashtun Candidate (Higher Educated)"))

figure33 <- ggplot(data = as.data.frame(ethnicity),
                    aes(x = estimate,
                        y = group)) + 
                      geom_point(size = 3)+ 
                        geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")

figure33

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b7_pashtun.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 34: Non-Pashtun Preferences Across Qualifications (Differences in MMs)

nonpashtun$high_educated_reorder <- relevel(nonpashtun$high_educated, ref = "Low")


ethnicity <- cj(nonpashtun, Choice ~ high_educated_reorder,
           id = ~ ID, estimate = "mm_differences",
           by = ~ Pashtun,
           level_order = "ascending") 


group <- c("Less Qualified Candidate", "High Qualified Candidate")


ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Less Qualified Candidate",
                             "High Qualified Candidate"))


figure34 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Mean Differences (Pashtun - Non-Pashtun)", y = "")+
            geom_vline(xintercept = 0, 
            linetype = "dashed")+
          ggtitle("")


# Export to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b8_pashtun_mm.pdf",
       width = 20, height = 13, units = "cm")


### SI Figure 35:  Non Tajiks preferences for Tajiks vs Non-Tajiks


nontajik <- data[data$Res_Tajik == 0, ]


ethnicity <- cj(nontajik, Choice ~ Tajik,
         id = ~ID,
         estimate= "mm")

group <- c("Non-Tajik Candidate", "Tajik Candidate")


ethnicity <- cbind(ethnicity, group)

figure35 <- ggplot(data = as.data.frame(ethnicity),
             aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
             linetype = "dashed")


# Export to Overleaf

figure35

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_2_tajik.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 36: Non-Tajiks's preferences across qualifications

ethnicity <- cj(nontajik, Choice ~ Education_Tajik_Leader,
           id = ~ID,
           estimate= "mm")


f.test.nontajik <- cj_anova(nontajik, Choice  ~ Tajik,
                        by = ~ Education_Tajik_Leader
)



group <- c("Non-Tajik Candidate (Higher Educated)", "Tajik Candidate (Higher Educated)",
           "Non-Tajik Candidate (Less Educated)", "Tajik Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Tajik Candidate (Higher Educated)",
                             "Tajik Candidate (Less Educated)",
                             "Non-Tajik Candidate (Less Educated)",
                             "Tajik Candidate (Higher Educated)"))

figure36 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
            linetype = "dashed")+
            ggtitle("")


figure36

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b8_tajik.pdf",
       width = 20, height = 13, units = "cm")

## SI Figure 37: Non-Tajiks' preferences across qualification (Marginal Means Differences)

nontajik$high_educated_reorder <- relevel(nontajik$high_educated, ref = "Low")


ethnicity <- cj(nontajik, Choice ~ high_educated_reorder,
           id = ~ ID, estimate = "mm_differences",
           by = ~ Tajik,
           level_order = "ascending") 


group <- c("Less Qualified Candidate", "High Qualified Candidate")


ethnicity  <- ethnicity %>%
              mutate(group = fct_relevel(group, "Less Qualified Candidate",
              "High Qualified Candidate"))


figure37 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
             xmax=estimate + 2*std.error))+
            labs(x="", y = "")+
            geom_vline(xintercept = 0, 
             linetype = "dashed")+
            ggtitle("")


figure37

# Export SI Figure 37 to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b9_tajik.pdf",
       width = 20, height = 13, units = "cm")



# SI Figure 38: Non-Turkem's Preferences

nonturk <- data[data$Res_Turkmen == 0, ]


ethnicity <- cj(nonturk, Choice ~ Turkmen,
         id = ~ID,
         estimate= "mm")

group <- c("Non-Turkmen Candidate", "Turkmen Candidate")


ethnicity <- cbind(ethnicity, group)

figure38 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
             y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
            linetype = "dashed")


figure38

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_2_turkmen.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 39: Non-Turkmen's Prefereces Across Qualifications


ethnicity <- cj(nonturk, Choice ~ Education_Turkmen_Leader,
            id = ~ID,
            estimate= "mm")


f.test.nonturk <- cj_anova(nonturk, Choice  ~ Turkmen,
                         by = ~ Education_Turkmen_Leader
)


group <- c("Non-Turkmen Candidate (Higher Educated)", "Turkmen Candidate (Higher Educated)",
           "Non-Turkmen Candidate (Less Educated)", "Turkmen Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Turkmen Candidate (Higher Educated)",
                             "Turkmen Candidate (Less Educated)",
                             "Non-Turkmen Candidate (Less Educated)",
                             "Turkmen Candidate (Higher Educated)"))

figure39 <- ggplot(data = as.data.frame(ethnicity),
                     aes(x = estimate,
                     y = group)) + 
                    geom_point(size = 3)+ 
                    geom_pointrange(aes(xmin= estimate - 2*std.error,
                    xmax=estimate + 2*std.error))+
                    labs(x="Marginal Means", y = "")+
                    geom_vline(xintercept = 0.5, 
                    linetype = "dashed")+
                    ggtitle("")



figure39

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b10_turkmen.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 40: Marginal Mean Differences with Non-Turkmen's Preferences

# Make Differences in MM plot

nonturk$high_educated_reorder <- relevel(nonturk$high_educated, ref = "Low")


ethnicity <- cj(nonturk, Choice ~ high_educated_reorder,
            id = ~ ID, estimate = "mm_differences",
            by = ~ Turkmen,
            level_order = "ascending") 


group <- c("Less Qualified Candidate", "High Qualified Candidate")


ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Less Qualified Candidate",
                             "High Qualified Candidate"))


figure40 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Mean Differences (Turkmen - Non-Turkmen)", y = "")+
            geom_vline(xintercept = 0, 
            linetype = "dashed")+
             ggtitle("")

figure40

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b11_turkmen.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 41: Non-Uzbeks' Preferences

nonuzbek <- data[data$Res_Uzbek == 0, ]

ethnicity <- cj(nonuzbek, Choice ~ Uzbek,
         id = ~ID,
         estimate= "mm")

group <- c("Non-Uzbek Candidate", "Uzbek Candidate")


ethnicity <- cbind(ethnicity, group)

figure41 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
            linetype = "dashed")


figure41

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_2_uzbek.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 42: Non-Uzbek's Ethnic Preferences When Qualifications are Incorporated


ethnicity <- cj(nonuzbek, Choice ~ Education_Uzbek_Leader,
            id = ~ID,
            estimate= "mm")


# F-test
f.test.nonuzbek <- cj_anova(nonuzbek, Choice  ~ Uzbek,
                         by = ~ Education_Uzbek_Leader
)


group <- c("Non-Uzbek Candidate (Higher Educated)", "Uzbek Candidate (Higher Educated)",
           "Non-Uzbek Candidate (Less Educated)", "Uzbek Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Uzbek Candidate (Higher Educated)",
                             "Uzbek Candidate (Less Educated)",
                             "Non-Uzbek Candidate (Less Educated)",
                             "Uzbek Candidate (Higher Educated)"))

figure42 <- ggplot(data = as.data.frame(ethnicity),
                  aes(x = estimate,
                  y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="Marginal Means", y = "")+
                  geom_vline(xintercept = 0.5, 
                  linetype = "dashed")+
                  ggtitle("")


figure42

# Export to Overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b12_uzbek.pdf",
       width = 20, height = 13, units = "cm")


## SI Figure 43: Non-Uzbek's MM Differnces Across Qualifications


nonuzbek$high_educated_reorder <- relevel(nonuzbek$high_educated, ref = "Low")


ethnicity <- cj(nonuzbek, Choice ~ high_educated_reorder,
            id = ~ ID, estimate = "mm_differences",
            by = ~ Uzbek,
            level_order = "ascending") 


group <- c("Less Qualified Candidate", "High Qualified Candidate")


ethnicity  <- ethnicity %>%
              mutate(group = fct_relevel(group, "Less Qualified Candidate",
              "High Qualified Candidate"))


figure43 <- ggplot(data = as.data.frame(ethnicity),
                  aes(x = estimate,
                  y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="Marginal Mean Differences (Uzbek - Non-Uzbek Candidates)", y = "")+
                  geom_vline(xintercept = 0, 
                  linetype = "dashed")+
                  ggtitle("")


figure43

# Export to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b13_uzbek.pdf",
       width = 20, height = 13, units = "cm")


## SI Section 7.02 In-Group Preferences from other groups


## SI Figure 44 Uzbek Respondents' Preferences


uzbek <- data[data$Res_Uzbek == 1, ] 


ethnicity <- cj(uzbek, Choice ~ Education_Uzbek_Leader,
           id = ~ID,
           estimate= "mm")


# F-Test results 
f.test.uzbek <- cj_anova(uzbek, Choice  ~ Uzbek,
                        by = ~ Education_Uzbek_Leader
)


f.test.uzbek.rating <- cj(uzbek, Rating ~ Education_Uzbek_Leader,
                     id = ~ID,
                     estimate= "mm")


group <- c("Non-Uzbek Candidate (Higher Educated)", "Uzbek Candidate (Higher Educated)",
           "Non-Uzbek Candidate (Less Educated)", "Uzbek Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Uzbek Candidate (Higher Educated)",
                             "Uzbek Candidate (Less Educated)",
                             "Non-Uzbek Candidate (Less Educated)",
                             "Uzbek Candidate (Higher Educated)"))

figure44 <- ggplot(data = as.data.frame(ethnicity),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
            xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
            linetype = "dashed")+
            ggtitle("")


figure44

# Export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_h.pdf",
       width = 20, height = 13, units = "cm")



# SI Figure 45:  Tajiks' preferences


tajik <- data[data$Res_Tajik == 1, ] 


ethnicity <- cj(tajik, Choice ~ Education_Tajik_Leader,
           id = ~ID,
           estimate= "mm")


# F-Test results 
f.test.tajik <- cj_anova(tajik, Choice  ~ Tajik,
                        by = ~ Education_Tajik_Leader
)


f.test.tajik.rating <- cj(tajik, Rating ~ Education_Tajik_Leader,
                     id = ~ID,
                     estimate= "mm")



group <- c("Non-Tajik Candidate (Higher Educated)", "Tajik Candidate (Higher Educated)",
           "Non-Tajik Candidate (Less Educated)", "Tajik Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Tajik Candidate (Higher Educated)",
                             "Tajik Candidate (Less Educated)",
                             "Non-Tajik Candidate (Less Educated)",
                             "Tajik Candidate (Higher Educated)"))

figure45 <- ggplot(data = as.data.frame(ethnicity),
                  aes(x = estimate,
                  y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="Marginal Means", y = "")+
                  geom_vline(xintercept = 0.5, 
                  linetype = "dashed")+
                  ggtitle("")

# export figure45 to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_k.pdf",
       width = 20, height = 13, units = "cm")



## SI Figure 46: Turkmen's Preferences


turk <- data[data$Res_Turkmen == 1, ] 


ethnicity<- cj(turk, Choice ~ Education_Turkmen_Leader,
           id = ~ID,
           estimate= "mm")


# F-Test results with choice
f.test.turk <- cj_anova(turk, Choice  ~ Turkmen,
                        by = ~ Education_Turkmen_Leader
)


f.test.turk.rating <- cj(turk, Rating ~ Education_Turkmen_Leader,
                     id = ~ID,
                     estimate= "mm")



group <- c("Non-Turkmen Candidate (Higher Educated)", "Turkmen Candidate (Higher Educated)",
           "Non-Turkmen Candidate (Less Educated)", "Turkmen Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Turkmen Candidate (Higher Educated)",
                             "Turkmen Candidate (Less Educated)",
                             "Non-Turkmen Candidate (Less Educated)",
                             "Turkmen Candidate (Higher Educated)"))

figure46 <- ggplot(data = as.data.frame(ethnicity),
                  aes(x = estimate,
                  y = group)) + 
                  geom_point(size = 3)+ 
                  geom_pointrange(aes(xmin= estimate - 2*std.error,
                  xmax=estimate + 2*std.error))+
                  labs(x="Marginal Means", y = "")+
                  geom_vline(xintercept = 0.5, 
                  linetype = "dashed")+
                  ggtitle("")

figure46

# Export to Overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h4_q.pdf",
       width = 20, height = 13, units = "cm")


